package com.app.controller.backstage;

import com.app.common.Result;
import com.app.mapper.PlanBusinessMapper;
import com.app.mapper.PlanMapper;
import com.app.model.*;
import com.app.service.*;
import com.app.vo.HomePageVo;
import com.app.vo.ValueAndNameVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Controller
@RestController
@RequestMapping("/api")
@ResponseBody
public class HomePageController {
    @Autowired
    private ProjectProgressService projectProgressService;
    @Autowired
    private KeyWorkService keyWorkService;
    @Autowired
    private PlanMapper planMapper;
    @Autowired
    private PlanBusinessMapper planBusinessMapper;
    @Autowired
    private StriveForFundsService striveForFundsService;
    @Autowired
    private AttractInvestmentService attractInvestmentService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private HomePageService homePageService;

    /**
     * 重点投资项目、重点工作任务、争取资金、招商引资
     *
     * @return
     */
    @GetMapping("/getCount")
    public Result<Object> getCount() {
        HomePageVo vo = new HomePageVo();
        Calendar calendar = Calendar.getInstance();
        String year = String.valueOf(calendar.get(Calendar.YEAR));
        //重点投资项目
        ProjectModel projectCount = projectProgressService.getProjectCount();
        vo.setThisYearProjectCount(projectCount.getThisYearCount());
        vo.setLastYearProjectCount(projectCount.getLastYearCount());
        //重点工作任务
        KeyWorkModel keyWorkCount = keyWorkService.getKeyWorkCount();
        vo.setThisYearKeyWorkCount(keyWorkCount.getThisYearCount());
        vo.setLastYearKeyWorkCount(keyWorkCount.getLastYearCount());
        //争取资金
        QueryWrapper<PlanFundsModel> planFundsModelQueryWrapper = new QueryWrapper<>();
        planFundsModelQueryWrapper.select("sum(task_plan) as sumAll");
        planFundsModelQueryWrapper.eq("year", year);
        PlanFundsModel planFundsModel = planMapper.selectOne(planFundsModelQueryWrapper);
        vo.setTaskAllCount(planFundsModel.getSumAll());
        StriveFundsModel model = striveForFundsService.selectOne();
        vo.setTaskEndAllCount(model.getSumAll());
        //招商引资
        QueryWrapper<PlanBusinessModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("sum(task_plan) as sumAll");
        queryWrapper.eq("year", year);
        PlanBusinessModel ord = planBusinessMapper.selectOne(queryWrapper);
        vo.setBusinessCount(ord.getSumAll());
        BusinessModel businessModel = attractInvestmentService.selectOne();
        vo.setBusinessEndCount(businessModel.getSumAll());
        return Result.success(vo);
    }

    //重点投资项目状态
    @GetMapping("/getStatusCount")
    public Result<Object> getStatusCount(String year) {
        ProjectModel projectModels = projectProgressService.selectCountByYear(year);
        List<ValueAndNameVo> vo = new ArrayList<>();
        int redCount = projectModels.getRedCount();
        int blueCount = projectModels.getBlueCount();
        int yellowCount = projectModels.getYellowCount();
        if (redCount == 0 && blueCount == 0 && yellowCount == 0) {
            return Result.success(null);
        }
        ValueAndNameVo vo1 = new ValueAndNameVo();
        vo1.setName("红色项目");
        vo1.setValue(redCount);
        vo.add(0, vo1);
        ValueAndNameVo vo2 = new ValueAndNameVo();
        vo2.setName("蓝色项目");
        vo2.setValue(blueCount);
        vo.add(1, vo2);
        ValueAndNameVo vo3 = new ValueAndNameVo();
        vo3.setName("黄色项目");
        vo3.setValue(yellowCount);
        vo.add(2, vo3);
        return Result.success(vo);
    }

    //重点投资项目类型
    @GetMapping("/getTypeCount")
    public Result<Object> getTypeCount(String year) {
        List<SystemModel> systemModelList = departmentService.getSystemByPid(1);
        for (int i = 0; i < systemModelList.size(); i++) {
            Integer integer = projectProgressService.selectCountByType(systemModelList.get(i).getId(), year);
            systemModelList.get(i).setValue(integer);
        }
        for (int i = 0; i < systemModelList.size(); i++) {
            if (systemModelList.get(i).getValue() != 0) {
                return Result.success(systemModelList);
            }
        }
        return Result.success(null);
    }

    //争取资金完成情况表 单位名称，年度计划，目前落实资金笔数，目前落实资金总额，已完成百分比
    @GetMapping("/getFundTable")
    public Result<Object> getFundTable() {
        List<PlanFundsModel> fundTable = striveForFundsService.getFundTable();
        return Result.success(fundTable);
    }

    //招商引资任务完成情况表
    @GetMapping("/getBusinessTable")
    public Result<Object> getBusinessTable() {
        List<PlanBusinessModel> fundTable = attractInvestmentService.getBusinessTable();
        return Result.success(fundTable);
    }

    //重点项目近五年各类型数量统计
    @GetMapping("/getTypeCountStatistics")
    public Result<Object> getTypeCountStatistics() {
        return Result.success(homePageService.getTypeCountStatistics());
    }

    //系统概览User Overview
    @GetMapping("/getUserOverview")
    public Result<Object> getUserOverview() {
        return Result.success(homePageService.getUserOverview());
    }
}
